package com.zhanglifan.action;


import com.zhanglifan.domain.Module;
import com.zhanglifan.domain.Role;
import com.zhanglifan.domain.User;
import com.zhanglifan.service.ModuleService;
import org.apache.shiro.SecurityUtils;
import org.apache.struts2.ServletActionContext;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

public class HomeAction extends BaseAction{
	private String moduleName;		//动态指定跳转的模块，在struts.xml中配置动态的result
	public String getModuleName() {
		return moduleName;
	}

	private ModuleService moduleService;

	public void setModuleService(ModuleService moduleService) {
		this.moduleService = moduleService;
	}

	public void setModuleName(String moduleName) {
		this.moduleName = moduleName;
	}

	public String fmain(){
		return "fmain";
	}
	
	public String title(){
		//获取session
		//User curUser = (User)session.get(SysConstant.CURRENT_USER_INFO);
		//ActionContext.getContext().getValueStack().push(curUser);

        List<Module> modules = moduleService.find("from Module where state=1 and ctype=0", Module.class, null);
        System.out.println("*** modules 的值是: " + modules + " ***");

        super.put("modules",modules);

        return "title";
	}

	//转向moduleName指向的模块
	public String tomain(){
		//获取request
//		String moduleName = (String)request.get("moduleName");
//
//		this.setModuleName(moduleName);
		return "tomain";
	}
	public String toleft(){
		//获取request
//		String moduleName = (String)request.get("moduleName");
//
//		this.setModuleName(moduleName);

        System.out.println("*** toleft 的值是: " + moduleName + " ***");
        System.out.println("*** toleft 的值是: " + moduleName + " ***");
        return "toleft";
	}


	public String jsonMenu() throws IOException {
        User user = (User) SecurityUtils.getSubject().getPrincipal();
        System.out.println("*** user 的值是: " + user + " ***");
        Set<Role> roles = user.getRoles();
        
        List<Module> moduleList = new ArrayList<>();
        StringBuilder sb = new StringBuilder();

        sb.append("[");
        for (Role role : roles ) {
            Set<Module> modules = role.getModules();
            for (Module m : modules) {
                if(moduleName.equals(m.getRemark())){
                    moduleList.add(m);

                    sb.append("{");
                    sb.append("\"id\":\"");
                    sb.append(m.getId());
                    sb.append("\",");
                    sb.append("\"pId\":\"");
                    sb.append(m.getParentId());
                    sb.append("\",");
                    sb.append("\"name\":\"");
                    sb.append(m.getName());
                    sb.append("\",");
                    sb.append("\"open\":\"");
                    sb.append("true");
                    sb.append("\"");
                    sb.append("},");
                }
            }
        }

        sb.deleteCharAt(sb.lastIndexOf(","));
        sb.append("]");



        System.out.println("*** sb 的值是: " + sb.toString() + " ***");
        
        System.out.println("*** moduleList 的值是: " + moduleList + " ***");

//        String jsonString = JSON.toJSONString(moduleList);
//
//        System.out.println("*** jsonString 的值是: " + jsonString + " ***");

        ServletActionContext.getResponse().getWriter().write(sb.toString());
        return NONE;
    }

}
